/*
 * @Author: Janssen000908 2601566046@qq.com
 * @Date: 2025-05-01 23:18:48
 * @LastEditors: Janssen000908 2601566046@qq.com
 * @LastEditTime: 2025-05-01 23:18:48
 * @FilePath: /learn38/LockGurad.hpp
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

/// 互斥锁的封装
#pragma once
#include <iostream>
#include <pthread.h>

using namespace std;

class Mutex
{
public:
    Mutex(pthread_mutex_t *lock)
        :_lock(lock)
    {}

    void Lock()
    {
        pthread_mutex_lock(_lock);
    }
    void Unlock()
    {
        pthread_mutex_unlock(_lock);
    }

    ~Mutex()
    {}
private:
    pthread_mutex_t *_lock;
};


// ?
class LockGuard
{
public:
    LockGuard(pthread_mutex_t* lock)
        :_mutex(lock)//这个时候_mutex对象已经存在
    {
        _mutex.Lock();
    }

    ~LockGuard()
    {
        _mutex.Unlock();
    }
private:
    Mutex _mutex;
};